﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using StudentManage.Common;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.SysManage.DataSynList
{
    public partial class JGXX_Syn : SecurityPage
    {
        private static DataTable dt_Add = new DataTable();
        private static DataTable dt_Up = new DataTable();
        private static List<string> Idlist = new List<string>();

        private void DropBanbing()
        {
            List<xy_bmxbm> xylist = xy_bmxbm.Where("BMFQID=@0", "03").ToList();
            this.Drop_xy.DataSource = xylist.OrderBy(p => p.BMDM).ToList();
            this.Drop_xy.DataValueField = "BMDM";
            this.Drop_xy.DataTextField = "BMMC";
            this.Drop_xy.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            this.DropBanbing();
        }

        //绑定要插入的数据
        private void RepInsertBind()
        {
            if (dt_Add == null || dt_Add.Rows.Count <= 0)
            {
                this.Rep_Insert.DataSource = null;
                this.Rep_Insert.DataBind();
            }
            else
            {
                int RowCount = dt_Add.Rows.Count;
                this.AspNetPager_Insert.RecordCount = RowCount;
                int PageSize = this.AspNetPager_Insert.PageSize;
                int SkipCount = PageSize * (this.AspNetPager_Insert.CurrentPageIndex - 1);

                List<DataRow> list = dt_Add.Rows.Cast<DataRow>().Skip(SkipCount).Take(PageSize).ToList();
                this.Rep_Insert.DataSource = list.CopyToDataTable();
                this.Rep_Insert.DataBind();
            }
        }

        //绑定要修改的数据
        private void RepUpdateBind()
        {
            if (dt_Up == null || dt_Up.Rows.Count <= 0)
            {
                this.Rep_Update.DataSource = null;
                this.Rep_Update.DataBind();
            }
            else
            {
                int RowCount = dt_Up.Rows.Count;
                this.AspNetPager_Update.RecordCount = RowCount;
                int PageSize = this.AspNetPager_Update.PageSize;
                int SkipCount = PageSize * (this.AspNetPager_Update.CurrentPageIndex - 1);

                List<DataRow> list = dt_Up.Rows.Cast<DataRow>().Skip(SkipCount).Take(PageSize).ToList();
                this.Rep_Update.DataSource = list.CopyToDataTable();
                this.Rep_Update.DataBind();
            }

        }

        //检测数据
        protected void Btn_UpLoad_Click(object sender, EventArgs e)
        {
            if (this.Drop_xy.SelectedItem == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(),"Alert","<script>alert('请选择学院!');</script>");
                return;
            }

            string SQLWhere = " where xymc='" + this.Drop_xy.SelectedItem.Text + "'";

            new DataSyn().GetData("xy_jgxx", "ZGH", ref dt_Add, ref dt_Up, ref Idlist, "BMDM", SQLWhere);
            this.lab_InCount.Text = (dt_Add != null ? dt_Add.Rows.Count.ToString() : "0");
            this.lab_UpCount.Text = (dt_Up != null ? dt_Up.Rows.Count.ToString() : "0");
            this.Btn_Exec.Enabled = true;
            this.lab_status.Text = "检测完毕";
            this.RepInsertBind();
            this.RepUpdateBind();
        }

        //同步数据
        protected void Btn_Exec_Click(object sender, EventArgs e)
        {
            this.Btn_Exec.Enabled = false;

            #region 用户表
            DataTable dt_user = new DataTable();
            dt_user.Columns.Add("id", typeof(int));
            dt_user.Columns.Add("XH", typeof(string));
            dt_user.Columns.Add("IsStudent", typeof(bool));
            dt_user.Columns.Add("UserName", typeof(string));
            dt_user.Columns.Add("PassWord", typeof(string));
            dt_user.Columns.Add("UName", typeof(string));
            dt_user.Columns.Add("Tel", typeof(string));
            dt_user.Columns.Add("QQ", typeof(string));
            dt_user.Columns.Add("Email", typeof(string));
            dt_user.Columns.Add("Specialty", typeof(string));
            dt_user.Columns.Add("Experience", typeof(string));
            dt_user.Columns.Add("Hobby", typeof(string));
            dt_user.Columns.Add("DataRole", typeof(int));
            dt_user.Columns.Add("RoleCode", typeof(string));
            dt_user.Columns.Add("RoleName", typeof(string));
            dt_user.Columns.Add("AddTime", typeof(DateTime));

            int MaxId = new DataSyn().GetMaxId();
            for (int i = 0; i < dt_Add.Rows.Count; i++)
            {
                DataRow row = dt_user.NewRow();
                row["id"] = MaxId;
                row["XH"] = dt_Add.Rows[i]["ZGH"].ToString();
                row["IsStudent"] = false;
                row["UserName"] = dt_Add.Rows[i]["ZGH"].ToString();
                row["PassWord"] = Utility.GetMD5PassWord(dt_Add.Rows[i]["ZGH"].ToString(), "");
                row["UName"] = dt_Add.Rows[i]["XM"].ToString();
                row["Tel"] = "";
                row["QQ"] = "";
                row["Email"] = "";
                row["Specialty"] = "";
                row["Experience"] = "";
                row["Hobby"] = "";
                row["DataRole"] = 12;//部门管理员
                row["RoleCode"] = "463915fcee193102";
                row["RoleName"] = "部门管理员";
                row["AddTime"] = DateTime.Now;
                dt_user.Rows.Add(row);
                MaxId += 1;
            }
            #endregion

            #region 教工表
            DataTable dt = new DataTable();
            dt.Columns.Add("ZGH", typeof(string));
            dt.Columns.Add("XM", typeof(string));
            dt.Columns.Add("XB", typeof(string));
            dt.Columns.Add("XYMC", typeof(string));
            dt.Columns.Add("BMMC", typeof(string));
            dt.Columns.Add("BMDM", typeof(string));
            dt.Columns.Add("CARDID", typeof(long));
            dt.Columns.Add("RYLB", typeof(string));
            for (int i = 0; i < dt_Add.Rows.Count; i++)
            {
                DataRow row = dt.NewRow();
                row["ZGH"] = dt_Add.Rows[i]["ZGH"].ToString();
                row["XM"] = dt_Add.Rows[i]["XM"].ToString();
                row["XB"] = dt_Add.Rows[i]["XB"].ToString();
                row["XYMC"] = dt_Add.Rows[i]["XYMC"].ToString();
                row["BMMC"] = dt_Add.Rows[i]["BMMC"].ToString();
                row["BMDM"] = dt_Add.Rows[i]["BMDM"].ToString();
                if (dt_Add.Rows[i]["CARDID"] != null && dt_Add.Rows[i]["CARDID"].ToString() != "")
                {
                    row["CARDID"] = Convert.ToInt64(dt_Add.Rows[i]["CARDID"].ToString());
                }
                row["RYLB"] = dt_Add.Rows[i]["RYLB"].ToString();
                dt.Rows.Add(row);
            }
            #endregion

            new DataSyn().ExecSqlstr("set IDENTITY_INSERT UserList on");
            new DataSyn().SqlBulkCopyInsert(dt_user, "UserList");
            new DataSyn().ExecSqlstr("set IDENTITY_INSERT UserList OFF");

            new DataSyn().SqlBulkCopyInsert(dt, "xy_jgxx");
            new DataSyn().UpdateData("xy_jgxx", "ZGH", dt_Up, Idlist, "BMDM");

            this.lab_status.Text = "执行完毕!";
            this.lab_InCount.Text = "";
            this.lab_UpCount.Text = "";
            this.Rep_Insert.DataSource = null;
            this.Rep_Insert.DataBind();
            this.Rep_Update.DataSource = null;
            this.Rep_Update.DataBind();
            this.AspNetPager_Insert.RecordCount = 0;
            this.AspNetPager_Update.RecordCount = 0;
        }

        //绑定事件
        protected void Rep_Insert_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string key = e.CommandArgument.ToString();
                dt_Add.Rows.Remove(dt_Add.Select("ZGH='" + key + "'").First());
                this.RepInsertBind();
            }
        }

        //绑定事件
        protected void Rep_Update_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string key = e.CommandArgument.ToString();
                dt_Up.Rows.Remove(dt_Up.Select("ZGH='" + key + "'").First());
                this.RepUpdateBind();
            }
        }

        protected void AspNetPager_Insert_PageChanged(object sender, EventArgs e)
        {
            this.RepInsertBind();
        }

        protected void AspNetPager_Update_PageChanged(object sender, EventArgs e)
        {
            this.RepUpdateBind();
        }
    }
}